package handler

import (
	"github.com/gin-gonic/gin"
	"happy-book/internal/web/response"
	"net/http"
)

// OK 处理成功相应
func OK(ctx *gin.Context, data any) {
	ctx.JSON(http.StatusOK, response.NewSuccessResp(data))
}

// ERR 处理失败响应
func ERR(ctx *gin.Context, code int, message string, data any) {
	ctx.JSON(http.StatusOK, response.NewRestResp(code, message, data))
}

// Uid 获取用户ID
// 如果获取不到则返回0
func Uid(ctx *gin.Context) int64 {
	//从Context中直接获取uid
	uid, ok := ctx.Get(UidKey)
	if !ok { //未登录,拒绝访问
		return 0
	}
	return uid.(int64)
}
